﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Confessor
{
    class XFileActions
    {

        /// <summary>
        /// Read a text file and create an array with the IP's on a new line.
        /// </summary>
        /// <param name="file"></param>
        public void HostsFromFileToArray(ScanObject scanObject, string file)
        {
            XRegEx xRegEx = new XRegEx();
            string line;
            StreamReader sr = new StreamReader(file);
            try
            {
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Contains(","))
                    {
                        string[] lines = line.Split(',');
                        foreach (string s in lines)
                        {
                            ConfessorHostsObject confObj = new ConfessorHostsObject();
                            confObj.IP = s.Trim();

                            bool lookLikeIP = xRegEx.RegExTest(4, confObj.IP);
                            bool isIP = xRegEx.RegExTest(0, confObj.IP);
                            bool isWindowsHost = xRegEx.RegExTest(3, confObj.IP);

                            if (lookLikeIP)
                            {
                                if (isIP)
                                {
                                    scanObject.Hosts.Add(confObj);
                                }
                            }

                            if (isWindowsHost && lookLikeIP == false)
                            {
                                scanObject.Hosts.Add(confObj);
                            }
                        }
                    }
                    else
                    {
                        ConfessorHostsObject confObj = new ConfessorHostsObject();
                        confObj.IP = line.Trim();
                        bool lookLikeIP = xRegEx.RegExTest(4, confObj.IP);
                        bool isIP = xRegEx.RegExTest(0, confObj.IP);
                        bool isWindowsHost = xRegEx.RegExTest(3, confObj.IP);

                        if (lookLikeIP)
                        {
                            if (isIP)
                            {
                                scanObject.Hosts.Add(confObj);
                            }
                        }

                        if (isWindowsHost && lookLikeIP == false)
                        {
                            scanObject.Hosts.Add(confObj);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WriteToErrorLog(ex.Message, scanObject);
            }
            sr.Close();
        }

        public void MakeDirectory(string directory)
        {

            try
            {
                if (Directory.Exists(directory))
                {
                    System.Windows.Forms.MessageBox.Show(directory + " already exists.");
                    return;
                }
                DirectoryInfo dir = Directory.CreateDirectory(directory);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        public void WriteToErrorLog(string message, ScanObject scanObject)
        {
            lock (this)
            {
                bool append = true;
                string file = scanObject.ResultsDirectory + @"\ErrorLog.txt";
                StreamWriter sw = new StreamWriter(file, append);
                sw.Write(message + Environment.NewLine);
                sw.Close();
            }
        }

        public void WriteConfessorScript(List<string> lists, ScanObject scanObject)
        {
            lock (this)
            {
                bool append = true;
                string file = scanObject.ResultsDirectory + @"\confess.bat";
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
                foreach (string s in lists)
                {
                    StreamWriter sw = new StreamWriter(file, append);
                    sw.Write(s + Environment.NewLine);
                    sw.Close();
                }
            }
        }
    }
}
